#include <iostream>
#include <string>

#include "evnetplus/udp/udp_client.h"
#include "evnetplus/event_loop_thread.h"
#include "evnetplus/event_loop_thread_pool.h"
#include "evnetplus/event_watcher.h"

using namespace evnetplus;
using namespace evnetplus::udp;
using namespace std;

#define REMOTE_SERVER_ADDRESS "192.168.0.224:8080"
#define UDPCLIENT_NAME "udp-example"

int main(int argc, char const *argv[])
{
    EventLoopThread baseloopthread;
    baseloopthread.Start(true);

    UDPClient client(baseloopthread.loop(), REMOTE_SERVER_ADDRESS, UDPCLIENT_NAME);
    client.SetMessageCallback([](const MessagePtr& msg){
        SendMessage(msg);
    });
    client.Create();

    bool bEnableStop = false;
    SignalEventWatcher signalWatcher(SIGINT, baseloopthread.loop(), [&bEnableStop](){
        std::cout << "atched signal" << std::endl;
        bEnableStop = true;
    });
    signalWatcher.Init();
    signalWatcher.AsyncWait();

    while (!bEnableStop) {
        client.SendMsg("This is a evnetplus udp client.");

        std::this_thread::sleep_for(std::chrono::milliseconds(1000));
    }

    client.Destroy();
    baseloopthread.Stop(true);

    return 0;
}

